package lion.framework.core.db.anno;

import java.lang.annotation.Annotation;

import lion.dev.lang.MapJ;
import lion.framework.core.anno.AnnoProcessor;
import lion.framework.core.anno.AnnotationProcessorManager;
import lion.framework.core.anno.IAnnotationProcessor;
import lion.framework.core.db.DataSourceManager;
import lion.framework.core.db.datasource.IDataSourceProvider;

import org.apache.commons.lang.StringUtils;

@AnnoProcessor(DataSourceProvider.class)
public class DataSourceProviderAnnoProcessor implements IAnnotationProcessor {

	@SuppressWarnings("unchecked")
	@Override
	public void processe(MapJ ctx, Annotation anno) throws Exception {

		Class<?> clazz = (Class<?>) ctx.get(AnnotationProcessorManager.ANNO_CONTEXT_CLASS);
		if (!IDataSourceProvider.class.isAssignableFrom(clazz)) { return; }

		DataSourceProvider dsp = (DataSourceProvider) anno;
		if (StringUtils.isBlank(dsp.value())) { return; }

		DataSourceManager.registProvider(dsp.value(), (Class<IDataSourceProvider>) clazz);
	}
}
